import Vue from 'vue';
import { Router } from '@/common/utils/index.js';
import { needLoginPages, IMG_BASE_URL, MAP_WEB_KEY } from '@/common/config.js';
import amapFile from '@/libs/amap-wx.js';
const router = new Router({ needLoginPages });
Vue.mixin({
  data() {
    return {
      myAmapFun: null, // 高德地图实例
    }
  },
    computed: {
      imgBaseUrl() {
        return IMG_BASE_URL;
      },
      // 主题色
        // themeColor() {
        //     return this.$store.state.themeColor;
        // },
        // 主题背景色
        themeBgColor() {
            return this.$store.state.themeBgColor;
        },
        // 半透明主题背景色
        themeBgOpacityColor() {
            return this.$store.state.themeBgOpacityColor;
        },
        priceColor() {
          return this.$store.state.priceColor;
        },
        radioColor() {
            return this.$store.state.themeColor;
        },
        i18n() {
            return this.$t('defVal');
        },
        _top(){
          let h = uni.upx2px(110);
          let _h = h + this.StatusBar + 'px';
          console.log(_h)
          return _h;
        }
    },
    methods: {
        // 设置自定义头部
        setNavigationBarTitle(title) {
            uni.setNavigationBarTitle({
                title: title
            })
        },
        // 设置自定义底部
        setTabBarItem() {
            uni.setTabBarItem({
                index: 0,
                text: '首页',
            })
			uni.setTabBarItem({
			    index: 1,
			    text: '学校',
			})
            uni.setTabBarItem({
                index: 2,
                text: '发现',
            })
            uni.setTabBarItem({
                index: 3,
                text: '我的',
            })
            // uni.setTabBarItem({
            //     index: 3,
            //     text: global.i18n['我的'],
            // })
        },
        // 页面跳转
        handleJump(e) {

            let target = e.currentTarget || e.target,
                url = target.dataset.url || 'back',
                type = target.dataset.type;
            if (url == '' || url == '#') return;
            switch (type) {
                case 'SWITCH':
                    router.switchTab(url);
                    break;
                case 'REDIRECT':
                    router.redirectTo(url);
                    break;
                case 'RELAUNCH':
                    router.reLaunch(url);
                    break;
                case 'BACK':
                    router.navigateBack();
                    break;
                default:
                    router.navigateTo(url);
                    break;
            }
        },
        /**
         * @func 自动定位
         */
        autoGetLocation(fn) {
        	let _this = this;
          this.myAmapFun = new amapFile.AMapWX({ key: MAP_WEB_KEY });
        	uni.getLocation({
        		success: (adr) => {
        			this.myAmapFun.getRegeo({
        				location: adr.longitude + ',' + adr.latitude,
        				success: (data) => {
        					//成功回调
        					let regeoData = data[0].regeocodeData.addressComponent;
                  let location = {
                    province: regeoData.province,
                    provinceCode: regeoData.adcode.substr(0, 2) + '0000',
                    city: Array.isArray(regeoData.city) && regeoData.city.length == 0 ? regeoData.province : regeoData.city,
                    cityCode: regeoData.adcode.substr(0, 4) + '00',
                    district: regeoData.district,
                    districtCode: regeoData.adcode + '',
                    addressDesc: data[0].desc,
                    latitude: data[0].latitude,
                    longitude: data[0].longitude
                  }
        					fn(location);
        				},
        				fail(info) {
        					//失败回调
                  console.log(JSON.stringify('info', info))
        				}
        			});
        		},
            fail(err) {
              uni.showToast({
                icon: 'none',
                title: '请到设置页面开启定位权限!'
              })
            },
            complete: () => {
            }
        	});
        },
        /**
         * @func 逆地理编码
         * @param {Number} lat 纬度
         * @param {Number} lng 经度
         * @param {function} fn 成功回调的方法，参数为解析地址信息
         */
        getRegeo(lat, lng, fn) {
          let _this = this;
          this.myAmapFun = new amapFile.AMapWX({ key: MAP_WEB_KEY });
          this.myAmapFun.getRegeo({
          	location: lng + ',' + lat,
          	success: (data) => {
          		//成功回调
          		let regeoData = data[0].regeocodeData.addressComponent;
              let location = {
                province: regeoData.province,
                provinceCode: regeoData.adcode.substr(0, 2) + '0000',
                city: Array.isArray(regeoData.city) && regeoData.city.length == 0 ? regeoData.province : regeoData.city,
                cityCode: regeoData.adcode.substr(0, 4) + '00',
                district: regeoData.district,
                districtCode: regeoData.adcode + '',
                addressDesc: data[0].desc,
                latitude: data[0].latitude,
                longitude: data[0].longitude
              }
          		fn(location);
          	},
          	fail: info => {
          		//失败回调
          	}
          });
        }
    }
})
